package com.korustar.autumnal.codemodel;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sun.codemodel.JExpr;
import com.sun.codemodel.JExpression;

public class CodeModelUtil {
	private static final Logger LOGGER = LoggerFactory.getLogger( CodeModelUtil.class );
	public static final String TYPENAME_INT = "int";
	public static final String TYPENAME_BOOL = "bool";
	public static final String TYPENAME_STRING = "String";
	/**
	 * TODO: ensure uniqueness
	 * 
	 * @param beanDefinitionName
	 * @return returns a member name without punctuation.
	 */
	public static String legaliseMemberName(String beanDefinitionName) {
		return beanDefinitionName.replace(".", "_").replace("#", "__").replace(":", "___").replace("-", "_");
	}

	public static JExpression guessValueType(Object value, String designatedType) {
		if(TYPENAME_INT.equals(designatedType)) {
			return JExpr.lit((Integer)value);
		} else if(TYPENAME_BOOL.equals(designatedType)) {
			return JExpr.lit((Boolean)value);
		} else if(TYPENAME_STRING.equals(designatedType)) {
			return JExpr.lit((String)value);
		} else if(designatedType==null) {
			String valueString= value.toString(); 
				if("true".equalsIgnoreCase(valueString) || "false".equalsIgnoreCase(valueString)) {
					return JExpr.lit(Boolean.valueOf(valueString));
				} else {
					LOGGER.warn("GUESSING value is a string, {}, {}", value, value.getClass());
					return JExpr.lit(valueString);
				}
		} else {
			LOGGER.warn("GUESSING value is a string, {}, {}", value, value.getClass());
			return JExpr.lit((String)value);
		}
	}
}
